/**
 * 研发项目数据模型
 */
const researchConstants = require('../config/research_constants');

class ResearchProjectModel {

	/**
	 * 获取数据库表结构
	 */
	static DB_STRUCTURE() {
		return researchConstants.PROJECT_SCHEMA;
	}

	/**
	 * 字段前缀
	 */
	static FIELD_PREFIX = 'PROJECT_';

	/**
	 * 状态字段名 - level命名法
	 */
	static LEVEL1_FIELD = 'level1';

	/**
	 * 子状态字段名 - level命名法
	 */
	static LEVEL2_FIELD = 'level2';

	/**
	 * 活动状态字段名 - level命名法
	 */
	static LEVEL3_FIELD = 'level3';

	/**
	 * 活动子状态字段名 - level命名法
	 */
	static LEVEL4_FIELD = 'level4';

	/**
	 * 集合名
	 */
	static COLLECTION_NAME = researchConstants.COLLECTION_NAMES.PROJECT;

	/**
	 * 状态选项用于UI展示
	 */
	static STATUS_OPTIONS = [
		{ label: '项目审核', val: researchConstants.PROJECT_STATUS.PROJECT_REVIEW, desc: '项目正在审核中' },
		{ label: '休眠', val: researchConstants.PROJECT_STATUS.DORMANT, desc: '项目处于休眠状态' },
		{ label: '活动', val: researchConstants.PROJECT_STATUS.ACTIVE, desc: '项目处于活动状态' },
		{ label: '已完成', val: researchConstants.PROJECT_STATUS.COMPLETED, desc: '项目已经完成' }
	];

	/**
	 * 子状态选项用于UI展示
	 */
	static SUB_STATUS_OPTIONS = {
		[researchConstants.PROJECT_STATUS.PROJECT_REVIEW]: [
			{ label: '董事长', val: researchConstants.PROJECT_REVIEW_SUB_STATUS.DIRECTOR, desc: '等待董事长审核' },
			{ label: '研发经理', val: researchConstants.PROJECT_REVIEW_SUB_STATUS.RD_MANAGER, desc: '等待研发经理审核' },
			{ label: '分配', val: researchConstants.PROJECT_REVIEW_SUB_STATUS.ASSIGNMENT, desc: '等待分配人员' }
		],
		[researchConstants.PROJECT_STATUS.DORMANT]: [
			{ label: '审核', val: researchConstants.DORMANT_SUB_STATUS.REVIEW, desc: '项目休眠审核中' },
			{ label: '归档', val: researchConstants.DORMANT_SUB_STATUS.ARCHIVE, desc: '项目已归档' }
		]
	};

	/**
	 * 活动状态选项用于UI展示
	 */
	static ACTIVE_STATE_OPTIONS = [
		{ label: '硬件确认', val: researchConstants.ACTIVE_STATE.HARDWARE_CONFIRM, desc: '硬件确认中' },
		{ label: '硬件', val: researchConstants.ACTIVE_STATE.HARDWARE, desc: '硬件开发中' },
		{ label: '调试', val: researchConstants.ACTIVE_STATE.DEBUG, desc: '调试中' },
		{ label: '资料同步', val: researchConstants.ACTIVE_STATE.DATA_SYNC, desc: '资料同步中' },
		{ label: '测试', val: researchConstants.ACTIVE_STATE.TESTING, desc: '测试中' },
		{ label: '客户确认', val: researchConstants.ACTIVE_STATE.CUSTOMER_CONFIRM, desc: '客户确认中' },
		{ label: '订单准备', val: researchConstants.ACTIVE_STATE.ORDER_PREPARATION, desc: '订单准备中' },
		{ label: '生产', val: researchConstants.ACTIVE_STATE.PRODUCTION, desc: '生产中' },
		{ label: '待反馈', val: researchConstants.ACTIVE_STATE.PRODUCT_FEEDBACK, desc: '待产品反馈' }
	];

	/**
	 * 活动子状态选项用于UI展示
	 */
	static ACTIVE_SUB_STATE_OPTIONS = {
		[researchConstants.ACTIVE_STATE.HARDWARE]: [
			{ label: '设计', val: researchConstants.HARDWARE_SUB_STATE.DESIGN, desc: '设计中' },
			{ label: '审核', val: researchConstants.HARDWARE_SUB_STATE.REVIEW, desc: '审核中' },
			{ label: '打样', val: researchConstants.HARDWARE_SUB_STATE.SAMPLE, desc: '打样中' },
			{ label: '制样', val: researchConstants.HARDWARE_SUB_STATE.PRODUCTION_SAMPLE, desc: '制样中' },
			{ label: '在库', val: researchConstants.HARDWARE_SUB_STATE.IN_STOCK, desc: '在库中' }
		],
		[researchConstants.ACTIVE_STATE.TESTING]: [
			{ label: '测试中', val: researchConstants.TESTING_SUB_STATE.TESTING, desc: '测试进行中' },
			{ label: '免测审核', val: researchConstants.TESTING_SUB_STATE.NO_TEST_REVIEW, desc: '免测审核中' }
		],
		[researchConstants.ACTIVE_STATE.CUSTOMER_CONFIRM]: [
			{ label: '寄样', val: researchConstants.CUSTOMER_CONFIRM_SUB_STATE.DELIVERY, desc: '样品寄送中' },
			{ label: '测试', val: researchConstants.CUSTOMER_CONFIRM_SUB_STATE.TESTING, desc: '客户测试中' },
			{ label: '无签样审核', val: researchConstants.CUSTOMER_CONFIRM_SUB_STATE.NO_SAMPLE_REVIEW, desc: '无签样审核中' },
			{ label: '样品在库', val: researchConstants.CUSTOMER_CONFIRM_SUB_STATE.SAMPLE_IN_STOCK, desc: '样品已入库' },
			{ label: '归档', val: researchConstants.CUSTOMER_CONFIRM_SUB_STATE.ARCHIVE, desc: '客户确认归档' }
		],
		[researchConstants.ACTIVE_STATE.ORDER_PREPARATION]: [
			{ label: '待量产', val: researchConstants.ORDER_PREPARATION_SUB_STATE.WAITING, desc: '等待量产' },
			{ label: '审核', val: researchConstants.ORDER_PREPARATION_SUB_STATE.REVIEW, desc: '量产审核中' },
			{ label: '文控', val: researchConstants.ORDER_PREPARATION_SUB_STATE.DOC_CONTROL, desc: '文档控制中' },
			{ label: '补充资料', val: researchConstants.ORDER_PREPARATION_SUB_STATE.SUPPLEMENT, desc: '补充资料中' },
			{ label: '产前会', val: researchConstants.ORDER_PREPARATION_SUB_STATE.PRE_MEETING, desc: '产前会进行中' },
			{ label: '生产准备', val: researchConstants.ORDER_PREPARATION_SUB_STATE.PREPARATION, desc: '生产准备中' }
		],
		[researchConstants.ACTIVE_STATE.PRODUCTION]: [
			{ label: '生产中', val: researchConstants.PRODUCTION_SUB_STATE.PRODUCING, desc: '生产进行中' },
			{ label: 'PE处理', val: researchConstants.PRODUCTION_SUB_STATE.PE_HANDLING, desc: 'PE处理中' },
			{ label: '研发排查', val: researchConstants.PRODUCTION_SUB_STATE.RD_CHECKING, desc: '研发排查中' },
			{ label: '完成', val: researchConstants.PRODUCTION_SUB_STATE.COMPLETED, desc: '生产完成' }
		]
	};

	/**
	 * 项目分类选项用于UI展示
	 */
	static CATE_OPTIONS = [
		{ label: '硬件项目', val: researchConstants.PROJECT_CATE.HARDWARE },
		{ label: '软件项目', val: researchConstants.PROJECT_CATE.SOFTWARE },
		{ label: '综合项目', val: researchConstants.PROJECT_CATE.INTEGRATED },
		{ label: '研究项目', val: researchConstants.PROJECT_CATE.RESEARCH },
		{ label: '其他项目', val: researchConstants.PROJECT_CATE.OTHER }
	];

	/**
	 * 项目优先级选项用于UI展示
	 */
	static PRIORITY_OPTIONS = [
		{ label: '低', val: researchConstants.PROJECT_PRIORITY.LOW },
		{ label: '中', val: researchConstants.PROJECT_PRIORITY.MEDIUM },
		{ label: '高', val: researchConstants.PROJECT_PRIORITY.HIGH }
	];

	/**
	 * 获取状态名称
	 * @param {number} status 状态值
	 * @returns {string} 状态名称
	 */
	static getStatusName(status) {
		const option = ResearchProjectModel.STATUS_OPTIONS.find(o => o.val === status);
		return option ? option.label : '未知';
	}

	/**
	 * 获取子状态名称
	 * @param {number} status 主状态值
	 * @param {string} subStatus 子状态值
	 * @returns {string} 子状态名称
	 */
	static getSubStatusName(status, subStatus) {
		if (!subStatus) return '';
		
		const options = ResearchProjectModel.SUB_STATUS_OPTIONS[status];
		if (!options) return '';
		
		const option = options.find(o => o.val === subStatus);
		return option ? option.label : '未知';
	}

	/**
	 * 获取活动状态名称
	 * @param {string} activeState 活动状态值
	 * @returns {string} 活动状态名称
	 */
	static getActiveStateName(activeState) {
		if (!activeState) return '';
		
		const option = ResearchProjectModel.ACTIVE_STATE_OPTIONS.find(o => o.val === activeState);
		return option ? option.label : '未知';
	}

	/**
	 * 获取活动子状态名称
	 * @param {string} activeState 活动状态值
	 * @param {string} activeSubState 活动子状态值
	 * @returns {string} 活动子状态名称
	 */
	static getActiveSubStateName(activeState, activeSubState) {
		if (!activeSubState) return '';
		
		const options = ResearchProjectModel.ACTIVE_SUB_STATE_OPTIONS[activeState];
		if (!options) return '';
		
		const option = options.find(o => o.val === activeSubState);
		return option ? option.label : '未知';
	}

	/**
	 * 计算项目进度
	 * @param {Object} projectData 项目数据
	 * @returns {number} 项目进度（0-100）
	 */
	static calculateProgress(projectData) {
		// 根据状态计算进度
		const level1 = projectData[this.LEVEL1_FIELD];
		const level2 = projectData[this.LEVEL2_FIELD];
		const level3 = projectData[this.LEVEL3_FIELD];
		const level4 = projectData[this.LEVEL4_FIELD];
		
		// 已完成状态
		if (level1 === researchConstants.PROJECT_STATUS.COMPLETED) return 100;
		
		// 项目审核状态
		if (level1 === researchConstants.PROJECT_STATUS.PROJECT_REVIEW) {
			if (level2 === researchConstants.PROJECT_REVIEW_SUB_STATUS.DIRECTOR) return 5;
			if (level2 === researchConstants.PROJECT_REVIEW_SUB_STATUS.RD_MANAGER) return 10;
			if (level2 === researchConstants.PROJECT_REVIEW_SUB_STATUS.ASSIGNMENT) return 15;
			return 5;
		}
		
		// 休眠状态
		if (level1 === researchConstants.PROJECT_STATUS.DORMANT) return 20;
		
		// 活动状态
		if (level1 === researchConstants.PROJECT_STATUS.ACTIVE) {
			if (!level3) return 20;
			
			// 硬件确认阶段
			if (level3 === researchConstants.ACTIVE_STATE.HARDWARE_CONFIRM) return 25;
			
			// 硬件阶段
			if (level3 === researchConstants.ACTIVE_STATE.HARDWARE) {
				if (level4 === researchConstants.HARDWARE_SUB_STATE.DESIGN) return 30;
				if (level4 === researchConstants.HARDWARE_SUB_STATE.REVIEW) return 32;
				if (level4 === researchConstants.HARDWARE_SUB_STATE.SAMPLE) return 35;
				if (level4 === researchConstants.HARDWARE_SUB_STATE.PRODUCTION_SAMPLE) return 38;
				if (level4 === researchConstants.HARDWARE_SUB_STATE.IN_STOCK) return 40;
				return 35;
			}
			
			// 调试阶段
			if (level3 === researchConstants.ACTIVE_STATE.DEBUG) return 45;
			
			// 资料同步阶段
			if (level3 === researchConstants.ACTIVE_STATE.DATA_SYNC) return 50;
			
			// 测试阶段
			if (level3 === researchConstants.ACTIVE_STATE.TESTING) {
				if (level4 === researchConstants.TESTING_SUB_STATE.TESTING) return 55;
				if (level4 === researchConstants.TESTING_SUB_STATE.NO_TEST_REVIEW) return 58;
				return 55;
			}
			
			// 客户确认阶段
			if (level3 === researchConstants.ACTIVE_STATE.CUSTOMER_CONFIRM) {
				if (level4 === researchConstants.CUSTOMER_CONFIRM_SUB_STATE.DELIVERY) return 60;
				if (level4 === researchConstants.CUSTOMER_CONFIRM_SUB_STATE.TESTING) return 65;
				if (level4 === researchConstants.CUSTOMER_CONFIRM_SUB_STATE.NO_SAMPLE_REVIEW) return 68;
				if (level4 === researchConstants.CUSTOMER_CONFIRM_SUB_STATE.SAMPLE_IN_STOCK) return 70;
				if (level4 === researchConstants.CUSTOMER_CONFIRM_SUB_STATE.ARCHIVE) return 72;
				return 65;
			}
			
			// 订单准备阶段
			if (level3 === researchConstants.ACTIVE_STATE.ORDER_PREPARATION) {
				if (level4 === researchConstants.ORDER_PREPARATION_SUB_STATE.WAITING) return 75;
				if (level4 === researchConstants.ORDER_PREPARATION_SUB_STATE.REVIEW) return 76;
				if (level4 === researchConstants.ORDER_PREPARATION_SUB_STATE.DOC_CONTROL) return 78;
				if (level4 === researchConstants.ORDER_PREPARATION_SUB_STATE.SUPPLEMENT) return 80;
				if (level4 === researchConstants.ORDER_PREPARATION_SUB_STATE.PRE_MEETING) return 82;
				if (level4 === researchConstants.ORDER_PREPARATION_SUB_STATE.PREPARATION) return 84;
				return 80;
			}
			
			// 生产阶段
			if (level3 === researchConstants.ACTIVE_STATE.PRODUCTION) {
				if (level4 === researchConstants.PRODUCTION_SUB_STATE.PRODUCING) return 85;
				if (level4 === researchConstants.PRODUCTION_SUB_STATE.PE_HANDLING) return 88;
				if (level4 === researchConstants.PRODUCTION_SUB_STATE.RD_CHECKING) return 90;
				if (level4 === researchConstants.PRODUCTION_SUB_STATE.COMPLETED) return 95;
				return 85;
			}
			
			// 待反馈阶段
			if (level3 === researchConstants.ACTIVE_STATE.PRODUCT_FEEDBACK) return 98;
			
			return 50;
		}
		
		return 0;
	}

	/**
	 * 获取完整状态描述
	 * @param {Object} projectData 项目数据
	 * @returns {string} 状态描述文本
	 */
	static getFullStatusDesc(projectData) {
		if (!projectData) return '未知';

		const level1 = projectData[this.LEVEL1_FIELD];
		const level2 = projectData[this.LEVEL2_FIELD];
		const level3 = projectData[this.LEVEL3_FIELD];
		const level4 = projectData[this.LEVEL4_FIELD];
		
		// 获取状态名
		let statusName = ResearchProjectModel.getStatusName(level1);
		
		// 如果有子状态，添加子状态名
		if (level2) {
			const subStatusName = ResearchProjectModel.getSubStatusName(level1, level2);
			if (subStatusName) {
				statusName += `-${subStatusName}`;
			}
		}
		
		// 如果是活动状态且有活动状态字段，添加活动状态
		if (level1 === researchConstants.PROJECT_STATUS.ACTIVE && level3) {
			const activeStateName = ResearchProjectModel.getActiveStateName(level3);
			if (activeStateName) {
				statusName += `(${activeStateName})`;
				
				// 如果有活动子状态，添加活动子状态
				if (level4) {
					const activeSubStateName = ResearchProjectModel.getActiveSubStateName(level3, level4);
					if (activeSubStateName) {
						statusName += `-${activeSubStateName}`;
					}
				}
			}
		}
		
		return statusName;
	}
}

module.exports = {
	ResearchProjectModel
}; 